/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {store} from "@mall/api-services";
import {WhiteSpace, WingBlank} from "antd-mobile";
import * as _ from "lodash";
import * as React from "react";
import {RouteComponentProps, withRouter} from "react-router";
import Icon from "../icon";
import "./product-list.scss";

interface DisplayProductComponentProps extends RouteComponentProps {
    product: store.Product;
}

class DisplayProductComponent extends React.Component<DisplayProductComponentProps> {

    constructor(props: Readonly<DisplayProductComponentProps>) {
        super(props);

        // bind this.
        this.renderPrice = this.renderPrice.bind(this);
    }

    public render() {
        const {history, product} = this.props;
        return (
            <div className="display-product" onClick={() => history.push(`/products/${product.id}`)}>
                {this.renderImage()}
                <WingBlank>
                    <WhiteSpace size="xs"/>
                    <div className="display-product-name">{this.briefName()}</div>
                    <WhiteSpace/>
                    <div className="display-product-linear">
                        {this.renderPrice()}
                        <span className="display-product-actions">
                            <Icon type="ellipsis" size="xxs"/>
                        </span>
                    </div>
                    <WhiteSpace size="sm"/>
                </WingBlank>
            </div>
        );
    }

    private renderImage() {
        const {product} = this.props;
        const {firstImageUrl} = product;
        return (
            <img className="display-product-image" alt="" src={firstImageUrl}/>
        );
    }

    private renderPrice() {
        const {product} = this.props;
        const {price} = product;
        return (
            <span className="display-product-price">
                    <span style={{fontSize: '12px', marginRight: '2px'}}>¥</span>
                {price}
            </span>
        );
    }

    /**
     * Display product name.
     */
    private briefName() {
        const {product} = this.props;
        return _.toString(product.name).substring(0, 20);
    }
}

const DisplayProduct = withRouter(DisplayProductComponent);

interface ProductListProps {
    mode?: 'grid' | 'list',
    products?: store.Product[]
}

export default class ProductList extends React.Component<ProductListProps> {

    public render() {
        const chunkProducts = this.getGroupProducts();
        return (
            <div className="product-list">
                {
                    _.map(chunkProducts, (aProducts, index) => {
                        return (
                            <div key={index} className="product-list-column">
                                {_.map(aProducts, aProduct => <DisplayProduct key={aProduct.id} product={aProduct}/>)}
                            </div>
                        );
                    })
                }
            </div>
        );
    }

    private getGroupProducts = () => {
        const {products} = this.props;
        if (products === undefined) {
            return [];
        }


        const chunkProducts: store.Product[][] = [];
        const getChunkProducts = (index: number) => {
            if (chunkProducts[index] === undefined) {
                chunkProducts[index] = [];
            }
            return chunkProducts[index];
        };

        for (let i = 0; i < products.length;) {
            let aProduct = products[i++];
            if (aProduct) {
                getChunkProducts(0).push(aProduct);
            }
            aProduct = products[i++];
            if (aProduct) {
                getChunkProducts(1).push(aProduct);
            }
        }

        return chunkProducts;
    }
}
